package com.heima.schedule;

import com.heima.common.redis.CacheService;
import com.heima.model.schedule.dtos.Task;
import com.heima.model.schedule.pojos.Taskinfo;
import com.heima.schedule.service.TaskService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Date;
import java.util.Set;

@SpringBootTest(classes = ScheduleApplication.class)
@RunWith(SpringRunner.class)
public class test {

    @Autowired
    private TaskService taskService;

    @Autowired
    private CacheService cacheService;

    @Test
    public void test(){
        for (int i = 0; i < 5; i++) {
            Task task = new Task();
            task.setTaskType(100+i);
            task.setPriority(50);
            task.setParameters("test".getBytes());
            task.setExecuteTime(new Date().getTime()+500*i);

            long taskId = taskService.addTask(task);
            System.out.println("taskId为:"+taskId);
        }

    }

    @Test
    public void testDelete(){
        boolean b = taskService.deleteTask(1828786844464717826L);
        if (b){
            System.out.println("删除成功");
        }else {
            System.out.println("删除失败");
        }
    }

    @Test
    public void testPollTask(){
        Taskinfo taskinfo = taskService.poll(100, 50);
        System.out.println(taskinfo);
    }
    @Test
    public void testKeys(){
        Set<String> futureKeys = cacheService.scan("future_" + "*");
        for (String futureKey : futureKeys) {
            System.out.println("key为:"+futureKey);
            String[] split = futureKey.split("future");
            System.out.println("截取到的为："+split[1]);
        }
    }
}
